<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/4/24
 * Time: 13:59
 */

namespace app\admin\controller;

use app\admin\logic\code\ErrorCode;
use app\admin\logic\UploadLogic;
use think\facade\Request;

class Common
{

    /**
     * 刷新表单令牌
     */
    public function refresh_token(){
        $token = Request::token();
        $res['code'] = ErrorCode::$SUCCESS;
        $res['msg'] = 'success';
        $res['data'] = $token;
        return json($res);
    }

    /**
     * 修改状态
     */
    public function update_status(){
        $id =  Request::post('id');
        $table =  Request::post('table_name');
        $status = db($table)->where('id','eq',$id)->value('status');
        $status = $status?0:1;
        db($table)->where('id','eq',$id)->update(array('status'=>$status));
        exit(json_encode(array('code'=>200)));
    }

    /**
     * 上传图片
     */
    public function upload_img(){
        $fileError = $_FILES['file']['error'];
        if ($fileError == 4){
            $res['code'] = ErrorCode::FILENULL;
            $res['msg'] = '上传内容不能为空';
            return json($res);
        }
        $file = Request::file('file');
        $srcName = Request::post('name', 'common');
        // 移动到框架应用根目录/uploads/ 目录下
        $uploadLogic = new UploadLogic();
        $uploadRes = $uploadLogic->uploadImg($file, $srcName);
        return json($uploadRes);
    }

}